function swPETPath = PETConv(inputPath)

% PETConv accepts either DICOM , niftii or normalised/smoothed nifti as
% input. If it is DICOM, it first converts to nifti using medcon program ;
% then, the nifti is smoothed and normalised if necessary. The function
% returns path to the smoothed and normalized nifti file.

medconPath = cd;
medconPath = fullfile(medconPath, 'XMedCon', 'bin', 'medcon.exe');
if (nargin==0)
    [file,folder]=uigetfile({'*.IMA', 'DICOM (.IMA)';'*.nii', 'NIfTI (.nii)'} ...
        ,'Select PET-Scan');
    PETPath = [folder file]; 
else
    PETPath = inputPath;
end

[pathstr, name, ext] = fileparts(PETPath);
if strcmp (ext, '.IMA')
    PETPath = CreateNIfTI(PETPath, medconPath);
    [pathstr, name, ext] = fileparts(PETPath);
end
if strcmp (ext, '.img') || strcmp (ext, '.hdr') 
    PETPath = CreateNIfTI(PETPath, medconPath);
    [pathstr, name, ext] = fileparts(PETPath);
end
if ~strcmp (name(1:2), 'sw')
    if name(1)~='w' % .nii neither normalised nor smoothed
        % Normalise.
        wPETPath = fullfile(pathstr, ['w' name ext]);
        swPETPath = fullfile(pathstr, ['sw' name ext]);
        templatePath = fullfile (cd, 'templates', 'PET.nii');
        spm_normalise(templatePath, PETPath); 
        snPath = fullfile(pathstr, [name '_sn.mat']);
        spm_write_sn(PETPath,snPath);
        %spm_check_registration(char(PETPath, wPETPath));
        [pathstr, name, ext] = fileparts(PETPath);      
    else % .nii = normalised not smoothed
        wPETPath = PETPath;
        PETPath = fullfile(pathstr, [name(2:end) ext]);
        swPETPath = fullfile(pathstr, ['s' name ext]);
    end
    % Smoothing.
    spm_smooth(wPETPath, swPETPath, 8);
else % .nii already smoothed and normalised    
    swPETPath = PETPath;
    PETPath = fullfile(pathstr, [name(3:end) ext]);
    wPETPath = fullfile(pathstr, [name(2:end) ext]);
end

